package com.nj9you.sdk.dlg;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nj9you.sdk.utils.Utils;

/**
 * Created by ck01- on 2017/7/6.
 */

public class WebViewDialog extends Dialog implements View.OnClickListener {
    private WebView mWebView;
    private TextView mTitleView;
    private Params mParams;
    private ProgressBar mProgressBar;

    public WebViewDialog(Context context, Params params) {
        super(context, android.R.style.Theme_DeviceDefault_NoActionBar);
        mParams = params;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(Utils.getLayoutId(getContext(),
                "jy_dialog_webview"));
        initView();
        initData();
    }

    private void initView() {
        View back = findViewById(Utils.getId(getContext(), "btn_title_back"));
        back.setOnClickListener(this);
        mProgressBar = (ProgressBar) findViewById(Utils.getId(getContext(),
                "progressbar"));
        mTitleView = (TextView) findViewById(Utils.getId(getContext(), "text_title"));
        mWebView = (WebView) findViewById(Utils.getId(getContext(), "web_agreement"));
        mWebView.setWebChromeClient(mWebChromeClient);
        mWebView.setWebViewClient(mWebViewClient);
        mWebView.getSettings().setJavaScriptEnabled(true);
    }

    private void initData() {
        if (mParams != null) {
            String title = mParams.getTitle();
            if (!TextUtils.isEmpty(title)) {
                mTitleView.setText(title);
            }
            String url = mParams.getUrl();
            mWebView.loadUrl(url);
        }
        mProgressBar.setMax(100);
    }

    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress > 0 && newProgress < 100) {
                mProgressBar.setVisibility(View.VISIBLE);
                mProgressBar.setProgress(newProgress);
            } else {
                mProgressBar.setVisibility(View.GONE);
            }
        }
    };

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }
    };

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    public static class Params {
        private String title;
        private String url;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
